package com.cl.mybatis.learn.day01;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Mybatis002 {
    public static void main(String[] args) throws IOException {
        String resource = "day01/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 从SqlSessionFactory中获取SqlSession
        SqlSession session = sqlSessionFactory.openSession();
        try {
            //多种方式执行sql语句
            User test1 = session.selectOne("com.cl.mybatis.learn.day01.UserMapper.selectById", 7);
            System.out.println("第1次：" + test1);


            User test2 = session.selectOne(UserMapper.class.getName() + ".selectById", 7);
            System.out.println("第2次：" + test2);
            
            UserMapper mapper = session.getMapper(UserMapper.class);
            User test3 = mapper.selectById(7);
            System.out.println("第3次：" + test3);
        } finally {
            session.close();
        }
    }
}
